﻿using SSC.Models;
using System;
using System.Collections.Generic;

namespace SSC.Services
{
    /// <summary>
    /// 处理保养项目的增删改查
    /// </summary>
    public class MaintenanceService
    {
        // 获取所有保养项目
        public List<MaintenanceItemModel> GetAllItems()
        {
            string query = "SELECT Id, ItemName, CycleDays, LastMaintainAt, Description FROM tbMaintenanceItems";
            return App.DatabaseService.GetAll<MaintenanceItemModel>(query);
        }

        // 新增保养项目
        public void AddItem(MaintenanceItemModel item)
        {
            string query = @"
                INSERT INTO tbMaintenanceItems (ItemName, CycleDays, LastMaintainAt, Description)
                VALUES (@ItemName, @CycleDays, @LastMaintainAt, @Description)";

            bool success = App.DatabaseService.Execute(query, item);
            if (!success)
            {
                throw new Exception("新增保养项目失败");
            }
        }

        // 更新保养项目
        public void UpdateItem(MaintenanceItemModel item)
        {
            string query = @"
                UPDATE tbMaintenanceItems
                SET ItemName = @ItemName,
                    CycleDays = @CycleDays,
                    LastMaintainAt = @LastMaintainAt,
                    Description = @Description
                WHERE Id = @Id";

            bool success = App.DatabaseService.Execute(query, item);
            if (!success)
            {
                throw new Exception("更新保养项目失败");
            }
        }

        // 删除保养项目
        public void DeleteItem(int id)
        {
            string query = "DELETE FROM tbMaintenanceItems WHERE Id = @Id";
            bool success = App.DatabaseService.Execute(query, new { Id = id });
            if (!success)
            {
                throw new Exception("删除保养项目失败");
            }
        }
    }
}
